package com.ijiuchuang.jc_localization.tools.views;

import android.graphics.Color;
import android.support.v7.widget.AppCompatTextView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

/**
 * Created by 王伟 on 2016/6/28
 * Function : 评论回复控件
 */
public class CommentTextView {

    // 回复人
    private String userA;
    // 被回复人
    private String userB;
    // 回复内容
    private String commentText;
    // 人名字的颜色
    private int nameColor = 0xFF576B95;
    // 其他文字颜色
    private int defaultColor = 0xFF000000;

    // 控件
    private TextView textView;
    // TODO: 2016/6/29  添加对 AppCompatTextView 的支持
    private AppCompatTextView compatTextView;

    // 建造者模式
    public static Builder with(TextView textView) {
        return new Builder(textView);
    }

    public static final class Builder {
        // 设置默认
        private String userA = "张三";
        private String userB = "李四";
        private String commentText = "Hello World";

        private TextView textView;
        private CommentTextView commentTextView = new CommentTextView();

        public Builder(TextView textView) {
            this.textView = textView;
        }

        public Builder user(String userA) {
            this.userA = userA;
            return this;
        }

        public Builder reply(String userB) {
            this.userB = userB;
            return this;
        }

        public Builder say(String commentText) {
            this.commentText = commentText;
            return this;
        }

        public void show(CommentClickListener listener){
            commentTextView.setUserA(userA);
            commentTextView.setUserB(userB);
            commentTextView.setCommentText(commentText);
            commentTextView.setTextView(textView);
            commentTextView.show(listener);
        }
    }

    public void setUserA(String userA) {
        this.userA = userA;
    }

    public void setUserB(String userB) {
        this.userB = userB;
    }

    public void setCommentText(String commentText) {
        this.commentText = commentText;
    }

    public void setTextView(TextView textView) {
        this.textView = textView;
    }

    // 显示
    public void show(CommentClickListener listener) {
        textView.setTextColor(defaultColor);
        textView.setHighlightColor(Color.TRANSPARENT);
        textView.setMovementMethod(LinkMovementMethod.getInstance());

        // 关键，要注意每一块的长度
        SpannableString string = new SpannableString(userA + "回复" + userB + "：" + commentText);
        // 回复人以及被回复人的名字颜色
        string.setSpan(
                new ForegroundColorSpan(nameColor),
                0,
                userA.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        string.setSpan(
                new ForegroundColorSpan(nameColor),
                (userA + "回复").length(),
                (userA + "回复" + userB).length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        // 添加点击事件
        string.setSpan(new OnUserNameClick(userA, OnUserNameClick.USER_A_CLICK, listener), 0, userA.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        string.setSpan(new OnUserNameClick(userB, OnUserNameClick.USER_B_CLICK, listener), (userA + "回复").length(), (userA + "回复" + userB).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        string.setSpan(new OnUserNameClick(commentText, OnUserNameClick.COMMENT_CLICK, listener), (userA + "回复" + userB + "：").length(), string.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(string);
    }

    // 区域点击
    public class OnUserNameClick extends ClickableSpan {

        private CommentClickListener listener;
        private String text;
        private int state;

        public static final int USER_A_CLICK = 0;
        public static final int USER_B_CLICK = 1;
        public static final int COMMENT_CLICK = 2;

        public OnUserNameClick(String text, int state, CommentClickListener listener) {
            this.listener = listener;
            this.text = text;
            this.state = state;
        }

        @Override
        public void onClick(View widget) {
            switch (state) {
                case USER_A_CLICK:
                    listener.onUserAClick(text);
                    break;
                case USER_B_CLICK:
                    listener.onUserBClick(text);
                    break;
                case COMMENT_CLICK:
                    listener.onCommentClick(text);
                    break;
            }
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
            if (state == COMMENT_CLICK) {
                ds.setColor(defaultColor);
            } else {
                ds.setColor(nameColor);
            }
        }
    }

    // 监听
    public interface CommentClickListener {
        void onUserAClick(String userA);
        void onUserBClick(String userB);
        void onCommentClick(String comment);
    }
}
